<?php
namespace App\Admin\Extensions;
use Encore\Admin\Admin;

class ChangeCsState
{
    protected $id = '';
    protected $collector_id;
    protected $orderid;

    public function __construct($orderid, $verName, $admin_id)
    {
        $this->orderid = $orderid;
        $this->verName = $verName;
        $this->admin_id = $admin_id;
    }

    protected function script()
    {
        return <<<SCRIPT
 layui.use('layer', function(){
        var layer = layui.layer;
        $('.cuishoujs').click(function () {
            var that = this;
            var orderid = $(that).attr('data-orderid');
            var user_name = $(that).attr('data-name');
            var admin_id = $(that).attr('data-adminid');
           
            var index = layer.open({
                content: '/admin/cuishou/daihou',
                type: 2,
                title: '<\span style="color:red">'+user_name+'</\span>',
                area: ['300px', ''],
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var repayment_state = $(layero).find("iframe").contents().find("#setSchool").val();
                    var collector = $(layero).find("iframe").contents().find("#collector").val();
                    var cuishou_mon = 0;
                    var cuishou_method = '';
                    if(repayment_state == 4){
                        cuishou_mon = $(layero).find("iframe").contents().find(".mon").val();
                        cuishou_method = $(layero).find("iframe").contents().find(".remethod").val();
                    }
                    
                    $.ajax({
                        url: '/admin/cuishoujs?orderid='+orderid+'&repayment_state='+repayment_state+'&collector='+collector+'&cuishou_mon='+cuishou_mon+'&remethod='+cuishou_method,
                        type: 'get',
                        success: function (res) {
                            if(res.code) {
                                layer.msg(res.text)
                                layer.close(index);
                                return;
                            }
                            layer.msg(res.text);
                            layer.close(index);
                        }
                    })
                }
            });         
        });
    });
SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        return "&nbsp;<a href='javascript:void(0);'><button data-orderid='{$this->orderid}' data-name='{$this->verName}' data-adminid='{$this->admin_id}'  class='cuishoujs layui-btn btn-primary layui-btn-xs'>操作</button></a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
